﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using CPService.Models;

namespace CPService.Controllers
{
    public class ParliamentariansController : ApiController
    {
        private CPServiceContext db = new CPServiceContext();

        // GET: api/Parliamentarians
        public IQueryable<Parliamentarian> GetParliamentarians()
        {
            return db.Parliamentarians;
        }

        // GET: api/Parliamentarians/5
        [ResponseType(typeof(Parliamentarian))]
        public IHttpActionResult GetParliamentarian(int id)
        {
            Parliamentarian parliamentarian = db.Parliamentarians.Find(id);
            if (parliamentarian == null)
            {
                return NotFound();
            }

            return Ok(parliamentarian);
        }

        // PUT: api/Parliamentarians/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutParliamentarian(int id, Parliamentarian parliamentarian)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != parliamentarian.ParliamentarianId)
            {
                return BadRequest();
            }

            db.Entry(parliamentarian).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ParliamentarianExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Parliamentarians
        [ResponseType(typeof(Parliamentarian))]
        public IHttpActionResult PostParliamentarian(Parliamentarian parliamentarian)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Parliamentarians.Add(parliamentarian);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = parliamentarian.ParliamentarianId }, parliamentarian);
        }

        // DELETE: api/Parliamentarians/5
        [ResponseType(typeof(Parliamentarian))]
        public IHttpActionResult DeleteParliamentarian(int id)
        {
            Parliamentarian parliamentarian = db.Parliamentarians.Find(id);
            if (parliamentarian == null)
            {
                return NotFound();
            }

            db.Parliamentarians.Remove(parliamentarian);
            db.SaveChanges();

            return Ok(parliamentarian);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ParliamentarianExists(int id)
        {
            return db.Parliamentarians.Count(e => e.ParliamentarianId == id) > 0;
        }
    }
}